package com.example.testlrc;

import java.io.IOException;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private WordView mWordView;
    private List<Integer> mTimeList;
    private MediaPlayer mPlayer;

    @SuppressLint("SdCardPath")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mPlayer.stop();
                finish();
            }
        });

        mWordView = (WordView) findViewById(R.id.text);

        mPlayer = new MediaPlayer();
        mPlayer.reset();
        LrcHandle lrcHandler = new LrcHandle();
        try {
            String s = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/";
            lrcHandler.readLRC(s + "爸爸去哪儿.lrc");
            mTimeList = lrcHandler.getTime();
            mPlayer.setDataSource(s + "爸爸去哪儿.mp3");
            mPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
        final Handler handler = new Handler();
        mPlayer.start();
        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (mPlayer.isPlaying()) {
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            mWordView.invalidate();
                        }
                    });
                    try {
                        Log.i("lbb", "size = " + mTimeList.size());
                        Log.i("lbb", "i = " + mTimeList.get(i));
                        Log.i("lbb", "i+1 = " + mTimeList.get(i + 1));
                        Thread.sleep(mTimeList.get(i + 1) - mTimeList.get(i));
                    } catch (InterruptedException e) {
                    }
                    i++;
                    if (i == mTimeList.size() - 1) {
                        mPlayer.stop();
                        break;
                    }
                }
            }
        }).start();
    }
}